package tankgame01;

import java.util.Iterator;
import java.util.Vector;

public class BulletShot extends Bullet implements Runnable {
    public static Vector walls = new Vector();

    public static Vector getWalls() {
        return walls;
    }

    public static void setWalls(Vector walls) {
        BulletShot.walls = walls;
    }

    public BulletShot(int x, int y, int direct) {
        super(x, y, direct);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            switch (getDirect()) {
                case 0:
                    moveUp();
                    break;
                case 1:
                    moveRight();
                    break;
                case 2:
                    moveDown();
                    break;
                case 3:
                    moveLeft();
                    break;
            }
            if (!(getX() >= 0 && getY() >= 0 && getX() <= 1000 && getY() <= 750) && isLive) {
                isLive = false;
                break;
            }
            if (isTouchWall()) {
                isLive = false;
                break;
            }
        }
    }

    public  boolean isTouchWall() {
        synchronized (walls) {
            Iterator iterator = walls.iterator();
            while (iterator.hasNext()) {
                Wall wall = (Wall) iterator.next();
                if (getX() >= wall.getX() && getX() <= wall.getX() + 10 && getY() >= wall.getY() && getY() <= wall.getY() + 10) {
                    iterator.remove();
                    return true;
                }
                if (getX() + 10 >= wall.getX() && getX() + 10 <= wall.getX() + 10 && getY() >= wall.getY() && getY() <= wall.getY() + 10) {
                    iterator.remove();
                    return true;
                }
                if (getX() >= wall.getX() && getX() <= wall.getX() + 10 && getY() + 10 >= wall.getY() && getY() + 10 <= wall.getY() + 10) {
                    iterator.remove();
                    return true;
                }
                if (getX() + 10 >= wall.getX() && getX() + 10 <= wall.getX() + 10 && getY() + 10 >= wall.getY() && getY() + 10 <= wall.getY() + 10) {
                    iterator.remove();
                    return true;
                }
            }
        }
        return false;
    }
}
